package net.maku.framework.common.utils;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
public class DateUtils {
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static SimpleDateFormat chineseFormat = new SimpleDateFormat("yyyy年MM月dd日");
    public static SimpleDateFormat chineseFullFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
    private static SimpleDateFormat sdf = new SimpleDateFormat();
    private static String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date 日期
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 日期解析
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回Date
     */
    public static Date parse(String date, String pattern) {
        try {
            return new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 把yyyy-MM-dd类型的时间转成java.util.Date类型
     *
     * @param adateStrteStr 日期字符串
     * @return 返回转换成功的Date类型的日期
     */
    public static Date convertDate(String adateStrteStr) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            sdf.setLenient(false);
            date = sdf.parse(adateStrteStr);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return date;
    }

    /**
     * 把yyyy-MM-dd HH:mm:ss类型的时间转成java.util.Date类型
     *
     * @param dateTime 日期字符串
     * @return 返回转换成功的Date类型的日期
     */
    public static Date convertDateTime(String dateTime) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            sdf.setLenient(false);
            date = sdf.parse(dateTime);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return date;
    }

    /**
     * @param dateTime 日期字符串
     * @param formate  格式
     * @return 返回转换成功的Date类型的日期
     */
    public static Date convertDateTimeByFormate(String dateTime, String formate) {
        Date date = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(formate);
            sdf.setLenient(false);
            date = sdf.parse(dateTime);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return date;
    }


    /**
     * 根据格式返回日期
     *
     * @param format
     * @return
     */
    public static String getDate(String format) {
        sdf.applyPattern(format);
        return sdf.format(new Date());
    }

    /**
     * 根据格式,日期 返回日期
     *
     * @param format
     * @return
     */
    public static String formatDate(String format, Date date) {
        sdf.applyPattern(format);
        return sdf.format(date);
    }

    /**
     * 获取今天是星期几
     *
     * @return
     */
    public static String getWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 输入时期是否今天
     *
     * @param date
     * @return
     */
    public static boolean isToday(Date date) {
        if (date == null) {
            return false;
        }
        Calendar nowCalendar = Calendar.getInstance();
        nowCalendar.setTime(new Date());
        Calendar inputDate = Calendar.getInstance();
        inputDate.setTime(date);

        if (nowCalendar.get(Calendar.YEAR) == inputDate.get(Calendar.YEAR)
                && nowCalendar.get(Calendar.MONTH) == inputDate.get(Calendar.MONTH)
                && nowCalendar.get(Calendar.DAY_OF_MONTH) == inputDate.get(Calendar.DAY_OF_MONTH)) {
            return true;
        }
        return false;
    }

    /**
     * 获得当前时间
     *
     * @return
     */
    public static Timestamp getCurrentTime() {
        return new Timestamp(new Date().getTime());
    }


    /**
     * 两个日期相差的天数（非严格意义上的天数，只考虑到天，不考虑时分秒等）
     *
     * @param bfDate
     * @param afDate
     * @return
     */
    public static int getIntervalDays(Date bfDate, Date afDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(bfDate);
        int day1 = calendar.get(Calendar.DAY_OF_YEAR);

        calendar.setTime(afDate);
        int day2 = calendar.get(Calendar.DAY_OF_YEAR);

        return day2 - day1;

    }

    /**
     * 当前时间 value 之前
     *
     * @param d
     * @param field 时间类型
     * @param value
     * @return
     */
    public static Date getDateBefore(Date d, int field, int value) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(field, now.get(field) - value);
        return now.getTime();
    }

    /**
     * 几天前
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

    /**
     * 几天后
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }


    public static String resolveMonthName(int monthsAgo) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 计算目标月份
        LocalDate targetDate = currentDate.minusMonths(monthsAgo);

        // 格式化月份名称
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M月");

        // 如果目标月份在当前年度，则直接返回格式化后的月份名称
        if (targetDate.getYear() == currentDate.getYear()) {
            return targetDate.format(formatter);
        } else {
            // 否则，返回包含“去年”的格式化后的月份名称
            DateTimeFormatter yearFormatter = DateTimeFormatter.ofPattern("去年M月");
            return targetDate.format(yearFormatter);
        }
    }

    public static Date getStartTimeOfDay(Date date) {
        Calendar dateStart = Calendar.getInstance();
        dateStart.setTime(date);
        dateStart.set(Calendar.HOUR_OF_DAY, 0);
        dateStart.set(Calendar.MINUTE, 0);
        dateStart.set(Calendar.SECOND, 0);
        return dateStart.getTime();
    }

    public static Date getEndTimeOfDay(Date date) {
        Calendar dateEnd = Calendar.getInstance();
        dateEnd.setTime(date);
        dateEnd.set(Calendar.HOUR_OF_DAY, 23);
        dateEnd.set(Calendar.MINUTE, 59);
        dateEnd.set(Calendar.SECOND, 59);
        return dateEnd.getTime();
    }

    public static void main(String[] args) {

        System.out.println(resolveMonthName(13));
    }
}
